package br.com.wepa.webapps.orca.logica.negocio.menu;

import br.com.wepa.webapps.orca.controle.ControleWeb;
import br.com.wepa.webapps.orca.logica.modelo.Funcao;
import br.com.wepa.webapps.orca.logica.modelo.Menuitem;
import br.com.wepa.webapps.orca.visual.client.MenuItemVisual;

public class MontaMenuBean {
	private Menuitem item;

	private final String NO_ACTION = "";

	public MontaMenuBean(Menuitem item) {
		setItem(item);
	}

	public String getLabel() {
		return item.getNome();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof MontaMenuBean) {
			MontaMenuBean m = (MontaMenuBean) obj;
			if (this.item == null) {
				return this.item == m.item;
			} else {
				return this.item.getIdMenuItem() == item.getIdMenuItem();
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		return item.getIdMenuItem();
	}

	public MontaMenuBean getPai() {
		return item.getMenuitem() == null ? null : new MontaMenuBean(item
				.getMenuitem());
	}

	public MenuItemVisual criaMenuItemVisual() {
		MenuItemVisual menuItem = null;			
		if (item != null) {			
			if (item.getFuncao()!= null) {
				Funcao funcao = item.getFuncao();
				menuItem = new MenuItemVisual(item.getNome(), ControleWeb
							.mountActionCall(funcao.getPath(),
									funcao.getMethod(),true));	
				
				menuItem.setFilhos(MontaMenu.instanciaList(null));			
			}else{
				menuItem = new MenuItemVisual(item.getNome(), NO_ACTION);
				menuItem.setFilhos(MontaMenu.instanciaList(null));
			}
			menuItem.setOrdem(item.getOrdem());
		}
		return menuItem;
	}

	public String toString() {
		return "menuItem=" + item.getNome();
	}

	public Menuitem getItem() {
		return item;
	}

	public void setItem(Menuitem item) {
		this.item = item;
	}

	public int getOrdem() {
		return item.getOrdem();
	}

}
